package com.zy.annotation;


import lombok.Data;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author CarreyZhou
 * @Date: 2024/5/3 13:29
 */
@Data
public class TelephoneValidator implements ConstraintValidator<ValidTelephone, String> {

    private static final String REGEX = "^(13[0-9]|14[5-9]|15[0-3,5-9]|16[6]|17[0-8]|18[0-9]|19[8,9])\\d{8}$";


    @Override
    public void initialize(ValidTelephone constraintAnnotation) {
        //从注解中获取值，进行初始化
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        //校验手机号码是否合法
        //true表示合法，false表示不合法
        if (value == null || value.trim().length() == 0) {
            return true;
        }
        return value.matches(REGEX);
    }
}
